//
//  EaseMessageDisplayCell.m
//  Linker
//
//  Created by hhsoft on 16/7/8.
//  Copyright © 2016年 www.huahansoft.com. All rights reserved.
//

#import "EaseMessageDisplayCell.h"

CGFloat const EaseMessageDisplayCellPadding = 5;

@interface EaseMessageDisplayCell ()
@property (strong, nonatomic) UILabel *titleLabel;
@end

@implementation EaseMessageDisplayCell
+ (void)initialize
{
        // UIAppearance Proxy Defaults
    EaseMessageDisplayCell *cell = [self appearance];
    cell.titleLabelColor = [UIColor whiteColor];
    cell.titleLabelFont = [UIFont systemFontOfSize:14];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self _setupSubview];
    }
    
    return self;
}

#pragma mark - setup subviews

- (void)_setupSubview
{
    _titleLabel = [[UILabel alloc] init];
    if (nil==_titleLabelColor) {
        _titleLabelColor = [UIColor whiteColor];
    }
    if (nil==_titleLabelFont) {
        _titleLabelFont = [UIFont systemFontOfSize:14];
    }
        //_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.backgroundColor = [UIColor lightGrayColor];
    _titleLabel.textColor = _titleLabelColor;
    _titleLabel.layer.cornerRadius=3;
    _titleLabel.layer.masksToBounds=YES;
    _titleLabel.font = _titleLabelFont;
    [self.contentView addSubview:_titleLabel];
    
        // [self _setupTitleLabelConstraints];
}

#pragma mark - Setup Constraints

- (void)_setupTitleLabelConstraints
{
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseMessageDisplayCellPadding]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-EaseMessageDisplayCellPadding]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-EaseMessageDisplayCellPadding]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseMessageDisplayCellPadding]];
}

#pragma mark - setter

- (void)setTitle:(NSString *)title
{
    _title = title;
    _titleLabel.text = _title;
    CGRect rect= [_title boundingRectWithSize:CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_titleLabelFont} context:nil];
    CGSize titleSize=rect.size;
    _titleLabel.frame=CGRectMake((CGRectGetWidth([UIScreen mainScreen].bounds)-titleSize.width-6)/2, ([EaseMessageDisplayCell cellHeight]-20)/2, titleSize.width+6, 20);
}

- (void)setTitleLabelFont:(UIFont *)titleLabelFont
{
    _titleLabelFont = titleLabelFont;
    _titleLabel.font = _titleLabelFont;
}

- (void)setTitleLabelColor:(UIColor *)titleLabelColor
{
    _titleLabelColor = titleLabelColor;
    _titleLabel.textColor = _titleLabelColor;
}

#pragma mark - public

+ (NSString *)cellIdentifier
{
    return @"EaseMessageDisplayCell";
}
+ (CGFloat)cellHeight{
    return 36;
}
@end
